#include "junix.h"

int main(int argc, char **argv)
{
	if(mkfifo("XX",0644))
		err_sys("mkfifo");

	if(mkfifo("YY",0644))
		err_sys("mkfifo");

	int fd = open("XX",O_RDONLY);
	if(fd==-1)
		err_sys("open");

	int fd2 = open("YY",O_RDONLY);
	if(fd2==-1)
		err_sys("open");

	fd_set rds;
	struct timeval tv={0,0};
	int numfd= fd>fd2 ? fd : fd2;

	while(1)
	{
		FD_ZERO(&rds);
		FD_SET(fd,&rds);
		FD_SET(fd2,&rds);

		if( select(numfd,&rds,NULL,NULL,&tv) == -1 )
			err_sys("select");

		int i;
		ssize_t sz;
		char buf[1024];

		for(i=0;i<numfd;++i)
		{
			if(FD_ISSET(i,&rds))
			{
				if( (sz=read(i,buf,1024)) > 0)
				{
					buf[sz]='\0';
					if( write(STDOUT_FILENO,buf,sz) != sz )
						err_sys("write");
				}

				if(sz<0)
					err_sys("read");
			}
		}
	}

	return 0;
}

